<?php
include_once 'connection.php';

$username = $_POST['username'];
$pass1 = $_POST['password1'];
$pass2 = $_POST['password2'];
//cek jika username sudah ada
$result = mysql_query("SELECT COUNT(*) as jml FROM tuser where username='$username'");
$row = mysql_fetch_row($result);
if($row[0]>0){ 
    $json = '{success:false,errors:{username:"Please chose another username"},msg:"Invalid data"}';
    echo $json;
}else if($pass1!=$pass2){ //cek apakah pass1 == pass2
    $json = '{success:false,errors:{password1:"Retype Password not match",
        password2:"Retype Password not match"},msg:"Invalid data"}';
    echo $json;
}else{
    //insert to database
    $realname=$_POST['realname'];
    $jenkel = $_POST['jenkel'];
    $address = $_POST['address'];
    $province = $_POST['province'];
    $kabkota = $_POST['kabkota'];
    $dob = $_POST['dob'];
    $description = $_POST['description'];
    mysql_query("INSERT INTO tuser(username,password,realname,jenkel,addr,province_id,kabkota_id,dob,description) 
        VALUES('$username','$pass1','$realname',$jenkel,'$address',$province,$kabkota,'$dob','$description')");
    $json = '{success:true,msg:"Data saved!"}';
    echo $json;
}

?>
